package com.smart.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.smart.entity.SsSite;
import com.smart.entity.SysUser;
import javax.annotation.Resource;
import com.smart.mapper.SsSiteMapper;
import com.smart.service.SsSiteService;
import com.smart.service.UserService;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * @author lgb
 * @date 2020年6月12日 下午4:54:38
 * Copyright (C) 深圳工勘科技
 * @说明: 工地管理service
 **/
@Service
public class SsSiteServiceImpl extends BaseServiceImpl<SsSite,Integer>
         implements SsSiteService{

	@Resource
	private SsSiteMapper siteMapper;
	@Resource
	private UserService userService;
	/** 
	 * (non-Javadoc)
	 * @see: 分页查询
	 **/
	@Override
	@Transactional(readOnly=true)
	public IPage<SsSite> findPage(int pageNum,int pageSize){
		LambdaQueryWrapper<SsSite> wrapper=new LambdaQueryWrapper<>();
		return page(new Page<SsSite>(pageNum,pageSize),wrapper);
	}

	@Override
	@Transactional(rollbackFor=RuntimeException.class)
	public Integer updateSite(int id, SsSite site) {
		SsSite oldObj = this.baseMapper.selectById(id);
		BeanUtils.copyProperties(site, oldObj);
		return this.baseMapper.updateById(site);
	}

	@Override
	@Transactional(readOnly=true)
	public Map<String,Object> findSysUserOnSite(int siteId) {
		Map<String,Object> data = new HashMap<>();
		List<SysUser> onList = siteMapper.findSysUserOnSite(siteId);
		data.put("selected", onList);
		List<SysUser> allList= userService.findAll();
		data.put("all", allList);
		return data;
	}

	@Override
	@Transactional(rollbackFor=RuntimeException.class)
	public void deleteUserBySiteId(int siteId,Integer[] userIds) {
		siteMapper.deleteUserBySiteId(siteId);
		if(userIds.length!=0){
			for(Integer userId : userIds){
				siteMapper.insertSiteUserId(siteId, userId);
			}
		}
	}

	
	
}